package com.wenwei.paper.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.wenwei.paper.R;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.UserLoginBean;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;
import com.wenwei.paper.utils.AccountValidatorUtil;
import com.wenwei.paper.utils.RogerSPUtils;
import com.wenwei.paper.utils.TimeCountUtil;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/13.
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.input_phone)
    EditText inputPhone;
    @Bind(R.id.input_pw)
    EditText inputPw;
    @Bind(R.id.input_re_pw)
    EditText inputRePw;
    @Bind(R.id.input_code)
    EditText inputCode;
    @Bind(R.id.register_get_code)
    TextView registerGetCode;
    @Bind(R.id.user_login)
    TextView userLogin;
    @Bind(R.id.base_title)
    TextView baseTitle;

    private int mType = 1;//编辑类型 1：注册 2：修改密码

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void init() {
        super.init();
        mType = getIntent().getIntExtra("type", 1);
    }

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setTranslucent(this, 30);
        if (mType == 1) {
            baseTitle.setText("用户注册");
        }else {
            baseTitle.setText("修改密码");
            userLogin.setText("确认修改");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.register_get_code, R.id.user_login, R.id.base_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_get_code:
                if(checkSms()){
                    getSmsCode();
                }
                break;
            case R.id.user_login:
                if(checkInputInfo()){
                    if (mType == 1) {
                        userRegister();
                    } else {
                        updateUser();
                    }
                }
                break;
            case R.id.base_back:
                finish();
                break;
        }
    }


    //获取短信验证码
    private BaseSubscriber<ArrayList> smsCodeSubscriber;

    private void getSmsCode() {
        smsCodeSubscriber = new BaseSubscriber<ArrayList>(mContext) {
            @Override
            public void onSuccess(ArrayList integer) {
                showToast("验证码获取成功，请注意查收...");
                TimeCountUtil timeCountUtil = new TimeCountUtil(RegisterActivity.this, 60000, 1000, registerGetCode);
                timeCountUtil.start();
            }
        };
        ApiManager.getInstance().getSmsCode(smsCodeSubscriber, inputPhone.getText().toString(), mType);
    }

    //注册
    private BaseSubscriber<ArrayList> registerSubscriber;

    private void userRegister() {
        registerSubscriber = new BaseSubscriber<ArrayList>(mContext) {
            @Override
            public void onSuccess(ArrayList integer) {
                showToast("注册成功，立即登录体验吗...");
                goActivity(mContext, LoginActivity.class);
            }
        };
        try {
            JSONObject jb = new JSONObject();
            jb.put("phone", inputPhone.getText().toString());
            jb.put("password", inputPw.getText().toString());
            jb.put("password_confirm", inputRePw.getText().toString());
            jb.put("sms_code", inputCode.getText().toString());
            ApiManager.getInstance().userRegister(registerSubscriber, jb.toString());
        } catch (Exception e) {
        }
    }


    //修改
    private BaseSubscriber<UserLoginBean> updateSubscriber;

    private void updateUser() {
        updateSubscriber = new BaseSubscriber<UserLoginBean>(mContext) {
            @Override
            public void onSuccess(UserLoginBean userLoginBean) {
//                showToast("密码重置成功，请重新登录...");
//                goActivity(mContext, LoginActivity.class);

                RogerSPUtils.putObject(mContext, Constant.SP_USER_LOGIN_RESULT,userLoginBean);
                Toast.makeText(mContext,"密码修改成功，立即体验吧",Toast.LENGTH_SHORT).show();
                goActivity(mContext,HostActivity.class);
                finish();
            }
        };
        try {
            JSONObject jb = new JSONObject();
            jb.put("phone", inputPhone.getText().toString());
            jb.put("password", inputPw.getText().toString());
            jb.put("password_confirm", inputRePw.getText().toString());
            jb.put("sms_code", inputCode.getText().toString());
            jb.put("registration_id", JPushInterface.getRegistrationID(mContext));
            ApiManager.getInstance().userUpdate(updateSubscriber, jb.toString());
        } catch (Exception e) {
        }
    }

    private boolean checkSms(){
        if(!inputPhone.getText().toString().equals("") && AccountValidatorUtil.isMobile(inputPhone.getText().toString())){
            return true;
        }else{
            showToast("请输入手机号");
            return false;
        }
    }

    private boolean checkInputInfo(){
        if(inputPhone.getText().toString().equals("") || inputPw.getText().toString().equals("")
                || inputRePw.getText().toString().equals("") || inputCode.getText().toString().equals("")){
            showToast("输入信息不完整...");
            return false;
        }else if (!inputRePw.getText().toString().equals(inputPw.getText().toString())){
            showToast("前后密码不一致，请再次确认...");
            return false;
        }else {
            return true;
        }
    }


}
